8 Algoritmo de Kruskal, para encontrar el árbol de recubrimiento de mínima suma.
12 int start
, end
, weight
;
13 bool operator < (const edge
&that
) const {
14 //Si se desea encontrar el árbol de recubrimiento de máxima suma, cambiar el < por un >
15 return weight
< that
.weight
;
21 int p
[10001], rank
[10001];
22 void make_set(int x
){ p
[x
] = x
, rank
[x
] = 0; }
23 void link(int x
, int y
){ rank
[x
] > rank
[y
] ? p
[y
] = x
: p
[x
] = y
, rank
[x
] == rank
[y
] ? rank
[y
]++: 0; }
24 int find_set(int x
){ return x
!= p
[x
] ? p
[x
] = find_set(p
[x
]) : p
[x
]; }
25 void merge(int x
, int y
){ link(find_set(x
), find_set(y
)); }
39 cin
>> t
.start
>> t
.end
>> t
.weight
;
43 sort(e
.begin(), e
.end());
44 for (int i
=0; i
<=n
; ++i
){
47 for (int i
=0; i
<e
.size(); ++i
){
48 int u
= e
[i
].start
, v
= e
[i
].end
, w
= e
[i
].weight
;
49 if (find_set(u
) != find_set(v
)){
50 //printf("Joining %d with %d using weight %d\n", u, v, w);
55 cout
<< total
<< endl
;